<template>
  <el-dialog
      :title="type === 'add' ? '添加角色' : '修改角色'"
      v-model="visible"
      width="800px"
      @close="handleClose"
  >
    <el-form :model="ruleForm" :rules="rules" ref="formRef" label-width="100px" class="menu-form">
      <el-form-item label="角色名称" prop="name">
        <el-input type="text" v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="角色Key" prop="rank">
        <el-input type="test" max='200' v-model="ruleForm.roleKey"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import {reactive, ref, toRefs} from 'vue'
import {useRoute} from 'vue-router'
import axios from '@/utils/axios'
import {ElMessage} from 'element-plus'

export default {
  name: 'DialogAddRole',
  props: {
    type: String,
    reload: Function
  },
  setup(props) {
    const formRef = ref(null)
    const route = useRoute()
    const state = reactive({
      visible: false,
      ruleForm: {
        name: '',
        roleKey: ''
      },
      rules: {
        name: [
          {required: 'true', message: '角色名称不能为空', trigger: ['change']}
        ],
        roleKey: [
          {required: 'true', message: '角色Key不能为空', trigger: ['change']}
        ]
      },
      id: ''
    })
    // 获取详情
    const getDetail = (id) => {
      axios.get(`/evaluate/role/${id}`).then(res => {
        state.ruleForm = {
          name: res.name,
          roleKey: res.roleKey
        }
      })
    }
    // 开启弹窗
    const open = (id) => {
      state.visible = true
      if (id) {
        state.id = id
        getDetail(id)
      } else {
        state.ruleForm = {
          name: '',
          roleKey: ''
        }
      }
    }
    // 关闭弹窗
    const close = () => {
      state.visible = false
    }
    const handleClose = () => {
      formRef.value.resetFields()
    }
    const submitForm = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          if (props.type === 'add') {
            axios.post('/evaluate/role', {
              name: state.ruleForm.name,
              roleKey: state.ruleForm.roleKey
            }).then(() => {
              ElMessage.success('添加成功')
              state.visible = false
              if (props.reload) props.reload()
            })
          } else {
            axios.post('/evaluate/role', {
              id: state.id,
              name: state.ruleForm.name,
              roleKey: state.ruleForm.roleKey
            }).then(() => {
              ElMessage.success('修改成功')
              state.visible = false
              if (props.reload) props.reload()
            })
          }
        }
      })
    }
    return {
      ...toRefs(state),
      open,
      close,
      formRef,
      submitForm,
      handleClose
    }
  }
}
</script>